package com.hua.io.old;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: _hua
 * @created: 2019/3/17.
 * @updater:
 * @description:
 */
public class Client {

    private static int num=8;
    public static void main(String[] args) {
        ExecutorService executorService= Executors.newFixedThreadPool(num);

        for(int i=0;i<num;i++){
            executorService.execute(new Runnable() {
                @Override
                public void run() {

                    sendRequest();
                }

                private void sendRequest() {

                    Socket mySocket= null;
                    try {
                        mySocket = new Socket("127.0.0.1",7700);
                        System.out.println("mySocket==========="+mySocket.toString()+Thread.currentThread().getName());

                        //写服务器
                        OutputStream outputstream = mySocket.getOutputStream();
                        String sendMsg = "当前请求线程为："+Thread.currentThread().getName();
                        outputstream.write(sendMsg.getBytes());

                        //读服务器
                        InputStream inputStream =mySocket.getInputStream();
                        byte[] b = new byte[1024];
                        int len =inputStream.read(b);
                        String msg = new String(b,0,len);
                        System.out.println("o222================"+msg);
                        mySocket.close();



                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("over=================");
                }
            });
        }

    }



}
